import {
  getLayout,
  getTodayOutput,
  getTodayPass,
  getTodayWork,
  getOee,
  getCoreMaterials,
  getExceptionMessage,
  getAnalysis,
} from './service';

export default {
  namespace: 'produceDashboard',
  state: {
    todayProduce: {},
    todayPass: {},
    todayWork: {},
    oee: {},
    coreData: [],
    analysisData: [],
    exceptionMessage: [],
  },

  effects: {
    *fetchLayoutAndInit(_: any, { call }: any) {
      const response = yield call(getLayout);
      return response;
    },
    *fetchTodayOutput(_: any, { call, put }: any) {
      const response = yield call(getTodayOutput);
      yield put({
        type: 'updateState',
        payload: {
          todayProduce: response,
        },
      });
    },
    *fetchTodayPass(_: any, { call, put }: any) {
      const response = yield call(getTodayPass);
      yield put({
        type: 'updateState',
        payload: {
          todayPass: response,
        },
      });
    },
    *fetchTodayWork(_: any, { call, put }: any) {
      const response = yield call(getTodayWork);
      yield put({
        type: 'updateState',
        payload: {
          todayWork: response,
        },
      });
    },
    *fetchOee(_: any, { call, put }: any) {
      const response = yield call(getOee);
      yield put({
        type: 'updateState',
        payload: {
          oee: response,
        },
      });
    },
    *fetchCoreMaterials(_: any, { call, put }: any) {
      const response = yield call(getCoreMaterials);
      yield put({
        type: 'updateState',
        payload: {
          coreData: response,
        },
      });
    },
    *fetchExceptionMessage(_: any, { call, put }: any) {
      const response = yield call(getExceptionMessage);
      yield put({
        type: 'updateState',
        payload: {
          exceptionMessage: response,
        },
      });
    },
    *fetchAnalysis(_: any, { call, put }: any) {
      const response = yield call(getAnalysis);
      yield put({
        type: 'updateState',
        payload: {
          analysisData: response,
        },
      });
    },
  },

  reducers: {
    updateState(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
